﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;

/// <summary>
/// Summary description for categoryBus
/// </summary>
public class categoryBus
{
    categoryDAL catDAL;
	public categoryBus()
	{
        catDAL = new categoryDAL();
		//
		// TODO: Add constructor logic here
		//
        
	}
    public string getCategories(int parent_id,int language_id)
    {
        DataTable dt = catDAL.getCategories(parent_id, language_id,true);
        string output = "";
        if (parent_id != 0)
        {
            output += "<ul class='art-nav'>";
        }
        foreach (DataRow r in dt.Rows)
        {
            output += "<li><a href='default.aspx?action=category&path=" + r["category_id"].ToString() + "'><span class='l'></span><span class='r'></span><span class='t'>" + r["name"].ToString() + "</span></a>";
            string children="";
            if (checkParentCategories(Convert.ToInt32(r["category_id"]), language_id))
            {
                children = getCategories(Convert.ToInt32(r["category_id"]), language_id);
            }
            output += children;
            output += "</li>";
        }
        if (parent_id != 0)
        {
            output += "</ul>";
        }
        return output;
    }
    public DataTable getCategoriesByParentId(int parent_id, int language_id)
    {
        return catDAL.getCategories(parent_id, language_id, true);
    }
    public bool checkParentCategories(int parent_id, int language_id)
    {
        DataTable dt = catDAL.getCategories(parent_id, language_id, true);
        if (dt.Rows.Count!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public DataTable getCategoriesByLanguageID(int language_id)
    {
        DataTable dt = catDAL.getCategoriesByLanguageID(language_id,true);
        return dt;
    }

    public ArrayList getCategoryByCategoryID(int category_id, int language_id)
    {
        ArrayList arr = new ArrayList();
        DataTable dt = catDAL.getCategoryNameByID(category_id, language_id, true);
        if (!dt.HasErrors&&dt.Rows!=null)
        {
            arr.Add(dt.Rows[0]["name"].ToString());
            arr.Add(dt.Rows[0]["parent_id"].ToString());
        }
        return arr;
    }

    public category_des getCategoryDescription(int category_id, int language_id)
    {
        category_des arr=null;
        DataTable dt = catDAL.getCategoryNameByID(category_id, language_id, true);
        if(dt!=null ||!dt.HasErrors){
            foreach(DataRow r in dt.Rows){
                  arr= new category_des(           
                              Convert.ToInt32(r["category_id"].ToString()),
                              Convert.ToInt32(r["language_id"].ToString()),
                              r["name"].ToString(),
                              r["meta_description"].ToString()            
                            );
            }
        }
        return arr;
       
    }

    public void DeleteCategoryByID(int category_id)
    {

        DataTable dt = catDAL.getCategoriesByParentID(category_id);
        foreach (DataRow r in dt.Rows)
        {
            DataTable check = catDAL.getCategoriesByParentID(Convert.ToInt32(r["category_id"].ToString()));
            if (!check.HasErrors)
            {
                //catDAL.DeleteCategoryByCategory_id(Convert.ToInt32(r["category_id"].ToString());
                this.DeleteCategoryByID(Convert.ToInt32(r["category_id"].ToString()));
            }
        }
        if (catDAL.DeleteCategoryByCategory_id(category_id)!=0)
        {

        }
    }

    public bool editcategory(int category_id, int language_id, string name, string des)
    {
        int i = catDAL.editCategory(category_id, language_id, name, des);
        if (i != 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool editCategoryData(int category_id, int parent_id, bool status, DateTime date_added)
    {
        int i = catDAL.editCategoryData(category_id, parent_id,status, date_added );
        if (i != 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public DataTable getAllCategory() {
        return catDAL.getAllCategory();
    }
    public int getLastCategoryID(int category_id)
    {
        DataTable dt = catDAL.getLastCategoryID(category_id);
        if (dt != null || !dt.HasErrors)
        {
            return Convert.ToInt32(dt.Rows[0]["category_id"].ToString());
        }
        else
        {
            return 0;
        }
    }
    public category getCategoryEntity(int category_id)
    {
        category cat = null;
        DataTable dt = catDAL.getCategoryEntity(category_id,true);
        if (dt != null || !dt.HasErrors)
        {
            foreach (DataRow r in dt.Rows)
            {
                cat = new category(
                                category_id,
                                Convert.ToInt32(r["parent_id"].ToString()),
                                Convert.ToBoolean(r["status"].ToString()),
                                Convert.ToDateTime(r["date_added"].ToString())
                                );
            }         
        }
        return cat;
    }
     
}